package com.ruoyi.pdf.receipt;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.pdf.PdfGenerateServiceImpl;
import com.ruoyi.system.domain.LabReceiptDetails;
import com.ruoyi.system.domain.ReceiptForm;
import com.ruoyi.system.service.ILabReceiptDetailsService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysUserService;

@Component
public class PdfGeneratorReceipt implements PdfGenerator {
	@Override
    public  String getFileName(Long instanceId){
    	return "南京邮电大学料单申报表("+instanceId+")";
    }
	@Autowired
    private PdfGenerateServiceImpl pdfGenerateService;
    @Autowired
    private ILabReceiptDetailsService labReceiptDetailsService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysDictDataService sysDictDataService;
    /* (non-Javadoc)
	 * @see com.ruoyi.pdf.receipt.PdfGenerator#run(java.lang.Long)
	 */
    @Override
	public void run(Long instanceId) throws Exception {
    	
    	LabReceiptDetails query = new LabReceiptDetails();
    	query.setInstanceId(instanceId);
    	ReceiptForm form = labReceiptDetailsService.selectLabReceiptDetailsList(query);
    	String userName = form.getCreateBy();
    	SysUser user = userService.selectUserByUserName(userName);
        Map<String, Object> data = new HashMap<>();
        data.put("user", user);
        //receiptTypeOpt
        data.put("receiptType", sysDictDataService.selectDictLabel("receiptTypeOpt", form.getReceiptTypeOpt()+""));
        //expectUseOpt
        data.put("expectUse", sysDictDataService.selectDictLabel("expectUseOpt", form.getExpectUseOpt()+""));
        SysDept dept = deptService.selectDeptById(form.getLabId());
        data.put("labName", dept.getDeptName());
        String voucherNo = form.getVoucherNo();
        if(voucherNo.indexOf(",")>0){
        	voucherNo =  voucherNo.replaceAll(",", ",<br/>");
        }else if(voucherNo.indexOf(";")>0){
        	voucherNo =  voucherNo.replaceAll(";", ";<br/>");
        }
        data.put("voucherNo", voucherNo);
        data.put("form", form);
        data.put("deviceItems", form.getDetailsList());
        pdfGenerateService.generatePdfFile("low_value_device", data, getFileName(instanceId)+".pdf");
    }
}
